import argparse

def parse_ch_group(value):
    """Custom type to parse comma-separated integers into a list."""
    try:
        return list(map(int, value.split(',')))
    except ValueError:
        raise argparse.ArgumentTypeError("ch_group must be comma-separated integers (e.g., '1,2,3,4')")

parser = argparse.ArgumentParser(description='device sample sn step in')
# Add ch_group with custom parsing (required positional argument)
parser.add_argument('ch_group', type=parse_ch_group, help='sample ch-group (comma-separated integers, e.g., "1,2,3,4")')
# Add pre_op_ret with default value -1 (optional argument)
parser.add_argument('--pre_op_ret', type=int, default=-1, help='pre-operation return code (default: -1)')

args = parser.parse_args()

# Example usage:
# python script.py "1,2,3,4" --pre_op_ret 0
# Access values with args.ch_group (list) and args.pre_op_ret (int)
print(f"ch_group: {args.ch_group} (type: {type(args.ch_group)})")
print(f"pre_op_ret: {args.pre_op_ret} (type: {type(args.pre_op_ret)})")


'''
runing result:
D:\git2025\20251112_python_args>python ./go.py 1,2,3
ch_group: [1, 2, 3] (type: <class 'list'>)
pre_op_ret: -1 (type: <class 'int'>)

D:\git2025\20251112_python_args>python ./go.py 1,2,3 0
usage: go.py [-h] [--pre_op_ret PRE_OP_RET] ch_group
go.py: error: unrecognized arguments: 0

D:\git2025\20251112_python_args>python ./go.py 1,2,3 -pre_op_ret 0
usage: go.py [-h] [--pre_op_ret PRE_OP_RET] ch_group
go.py: error: unrecognized arguments: -pre_op_ret 0

D:\git2025\20251112_python_args>python ./go.py 1,2,3 --pre_op_ret 0
ch_group: [1, 2, 3] (type: <class 'list'>)
pre_op_ret: 0 (type: <class 'int'>)

D:\git2025\20251112_python_args>python ./go.py 1,2,3 --pre_op_ret -1
ch_group: [1, 2, 3] (type: <class 'list'>)
pre_op_ret: -1 (type: <class 'int'>)

D:\git2025\20251112_python_args>python ./go.py 1 --pre_op_ret -1
ch_group: [1] (type: <class 'list'>)
pre_op_ret: -1 (type: <class 'int'>)

D:\git2025\20251112_python_args>python ./go.py 1 --pre_op_ret 0
ch_group: [1] (type: <class 'list'>)
pre_op_ret: 0 (type: <class 'int'>)
'''